package util;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import app.MyApplication;
import data.SharedPreferencesUtils;
import util.fileUtil.SelectFiles;


/**
 * Created by liuxiaobin on 2017/4/1
 * 计时类
 */

public class TimeUtil {
    private static TimeUtil mTimeUtil;
    private Context mContext;
    private String mType; //类型
    private Timer mTimer;//倒计时

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    if(mType.equals("LogUploads")){
                        LogUtil.eE("","收到"+"...."+NetWorkstateUtil.getInstance().isNetWorkstae());
                        if(!NetWorkstateUtil.getInstance().isNetWorkstae()){
                            try {
                                SelectFiles.getNewSelectFiles().getFileLogUpload(mContext, MyApplication.getApp().getFWQString());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
            }
        }
    };

    private TimeUtil(Context context){
        this.mContext = context;
    }

    public static TimeUtil getInstance(Context context){
        if(mTimeUtil == null){
            synchronized (TimeUtil.class){
                if (mTimeUtil == null){
                    mTimeUtil = new TimeUtil(context);
                    return mTimeUtil;
                }
            }
        }
        return mTimeUtil;
    }

    public void getShowTime(String type){
        mType = type;
        if(mTimer == null){ mTimer = new Timer();}

        TimerTask task = new TimerTask() {
            @Override public void run() {
                Message message = new Message();
                message.what = 1;
                mHandler.sendMessage(message);
            }
        };

        if(mType.equals("LogUploads") && !NetWorkstateUtil.getInstance().isNetWorkstae()){
            int idInt = Integer.parseInt((String) SharedPreferencesUtils.getParam(mContext,HomeUtil.mUseridDB,""));
            if(idInt <= 6000){
                mTimer.schedule(task,120*1000);
            }else if(idInt > 6000 && idInt <= 12000){
                mTimer.schedule(task,180*1000);
            }else if(idInt > 12000 && idInt <= 18000){
                mTimer.schedule(task,240*1000);
            }else if(idInt > 18000){
                mTimer.schedule(task,300*1000);
            }
        }
    }
}
